jjzjj

Java ExecutorService 和 ThreadPoolExecutor

全部标签

java - 如何在不关闭 Executor 的情况下等待 ThreadPoolExecutor 中的所有任务完成?

我不能使用shutdown()和awaitTermination()因为它可能会在等待时将新任务添加到ThreadPoolExecutor。所以我正在寻找一种方法,等待ThreadPoolExecutor清空它的队列并完成它的所有任务,而不停止在此之前添加新任务。如果有什么不同,这适用于Android。谢谢更新:很多周后,我重新审视了这个,发现修改后的CountDownLatch在这种情况下对我来说效果更好。我会保留答案,因为它更适用于我的问题。 最佳答案 如果您想知道某个任务或某批任务何时完成,您可以使用ExecutorServi

java - 有了ThreadPoolExecutor,如何获取线程池中运行的线程名称?

我在Java中使用ThreadPoolExecutor来管理大量正在运行的线程。我已经创建了我自己的简单ThreadFactory,这样我就可以给线程更好的名字。问题是名称是在线程池首次创建时在线程中设置的,并且与线程池实际运行的任务无关。我理解这一点……我的Runnable和Callable——尽管它们有名称——实际上是ThreadPoolExecutor运行线程的抽象级别。StackOverflow上还有一些关于为ThreadPoolExecutor线程池创建名称的其他问题。(参见HowtogivenametoacallableThread?和Howtonamethethreads

java - ThreadPoolExecutor 中的死锁

遇到ThreadPoolExecutor停在execute(Runnable)函数中,而所有ThreadPool线程都在等待getTaskfunc,workQueue为空。有人有什么想法吗?ThreadPoolExecutor使用ArrayBlockingQueue创建,并且corePoolSize==maximumPoolSize=4[编辑]更准确地说,线程在ThreadPoolExecutor.exec(Runnablecommand)func中被阻塞。它有要执行的任务,但没有执行。[Edit2]执行器在工作队列(ArrayBlockingQueue)的某处被阻塞。[Edit3]调

python - 检查 `concurrent.futures.ThreadPoolExecutor`

我有一个实时的concurrent.futures.ThreadPoolExecutor。我想检查它的状态。我想知道有多少个线程,有多少个正在处理任务,哪些任务,有多少是空闲的,哪些任务在队列中。我怎样才能找到这些东西? 最佳答案 池和待处理工作项队列有一些可见性。要找出可用的内容,请打印poolx.__dict__以查看结构。阅读ThreadPool代码,很不错:concurrent.futures.thread下面创建了一个线程池。然后它创建两个作业:一个休眠3秒,另一个立即返回。然后打印池中待处理工作项的数量。之后,我们从工作

python - 嵌套 concurrent.futures.ThreadPoolExecutor

我有一个程序,我目前正在使用concurrent.futures.ThreadPoolExecutor来同时运行多个任务。这些任务通常受I/O限制,涉及访问本地数据库和远程RESTAPI。但是,这些任务本身可以拆分为子任务,这也将受益于并发性。我希望在任务中使用concurrent.futures.ThreadPoolExecutor是安全的。我已经编写了一个玩具示例,它似乎可以工作:importconcurrent.futuresdefinner(i,j):returni,j,i**jdefouter(i):withconcurrent.futures.ThreadPoolExecu

python - ThreadPoolExecutor 中的 worker 并不是真正的守护进程

我想不通的是,尽管ThreadPoolExecutor使用守护进程,但即使主线程退出,它们仍会运行。我可以在python3.6.4中提供一个最小的例子:importconcurrent.futuresimporttimedeffn():whileTrue:time.sleep(5)print("Hello")thread_pool=concurrent.futures.ThreadPoolExecutor()thread_pool.submit(fn)whileTrue:time.sleep(1)print("Wow")主线程和工作线程都是死循环。因此,如果我使用KeyboardInt

python - 并发.futures.ThreadPoolExecutor.map() : timeout not working

importconcurrent.futuresimporttimedefprocess_one(i):try:print("dealingwith{}".format(i))time.sleep(50)print("{}Done.".format(i))exceptExceptionase:print(e)defprocess_many():withconcurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS)asexecutor:executor.map(process_one,range(100),timeout=3)

Python 2.7 concurrent.futures.ThreadPoolExecutor 不并行化

我在基于Inteli3的机器上运行以下代码,该机器具有4个虚拟内核(2个超线程/物理内核,64位)并安装了Ubuntu14.04:n=multiprocessing.cpu_count()executor=ThreadPoolExecutor(n)tuple_mapper=lambdai:(i,func(i))results=dict(executor.map(tuple_mapper,range(10)))代码似乎不是以并行方式执行的,因为CPU的利用率一直只有25%。在利用率图中,4个虚拟核心中只有一个被100%使用。使用的核心每10秒左右交替一次。但是并行化在具有相同软件设置的服

java - Android 中的新 Thread(task).start() VS ThreadPoolExecutor.submit(task)

在我的Android项目中,我有很多地方需要异步运行一些代码(网络请求、对数据库的调用等)。这不是长时间运行的任务(最多几秒钟)。到目前为止,我一直在做这种事情,创建一个新线程,将一个新的可运行对象传递给该任务。但最近我读了一篇关于Java中的线程和并发的文章,并明白为每个任务创建一个新线程并不是一个好的决定。所以现在我在我的Application类中创建了一个ThreadPoolExecutor,它拥有5个线程。这是代码:publicclassAppextendsApplication{privateThreadPoolExecutormPool;@Overridepublicvoi

python - ThreadPoolExecutor().map 与 ThreadPoolExecutor().submit 有何不同?

我只是对我编写的一些代码感到非常困惑。我惊讶地发现:withconcurrent.futures.ThreadPoolExecutor(max_workers=4)asexecutor:results=list(executor.map(f,iterable))和withconcurrent.futures.ThreadPoolExecutor(max_workers=4)asexecutor:results=list(map(lambdax:executor.submit(f,x),iterable))产生不同的结果。第一个生成f返回的任何类型的列表,第二个生成concurrent.